package com.totoro.tyrank.mapper.impl;

import com.totoro.tyrank.TyRank;
import com.totoro.tyrank.constants.StaticSystemVarConst;
import com.totoro.tyrank.data.PlayerData;
import com.totoro.tyrank.mapper.RankPlayerDataMapper;
import com.totoro.tyrank.utils.ListUtil;

import java.util.List;
import java.util.stream.Collectors;

public class RankPlayerDataMapperImpl implements RankPlayerDataMapper {
    @Override
    public List<PlayerData> selectAllPlayerDataByRankId(String rankId) {
        String sql = "select * from ty_rank_player_data where rank_id = ?";
        try {
            return TyRank.getBaseDao().baseQuery(sql, PlayerData.class, rankId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return StaticSystemVarConst.EMPTY_LIST;
    }

    @Override
    public List<String> selectAllPlayerIdentityByRankId(String rankId) {
        return selectAllPlayerDataByRankId(rankId).stream().map(PlayerData::getPlayerIdentity).collect(Collectors.toList());
    }

    @Override
    public Boolean selectIsHavePlayerByRankId(String playerIdentity, String rankId) {
        String sql = "select 1 from ty_rank_player_data where player_identity = ? and rank_id = ? limit 1";
        try {
            List<Long> singleQuery = TyRank.getBaseDao().singleQuery(sql, Long.class, playerIdentity, rankId);
            if(ListUtil.isNotBlank(singleQuery)) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public void insertRankPlayerData(PlayerData playerData) {
        String sql = "insert into ty_rank_player_data (player_identity, rank_id, ranking, current_value, reward_receive, update_date_time) values(?, ?, ?, ?, ?, ?)";
        try {
            TyRank.getBaseDao().executeUpdate(sql, playerData.getPlayerIdentity(), playerData.getRankId(), playerData.getRanking(),
                    playerData.getCurrentValue(), playerData.getRewardReceive().toString(), playerData.getUpdateDateTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateRankPlayerData(PlayerData playerData) {
        String sql = "update ty_rank_player_data set ranking = ?, current_value = ?, reward_receive = ?, update_date_time = ? where player_identity = ? and rank_id = ?";
        try {
            TyRank.getBaseDao().executeUpdate(sql, playerData.getRanking(), playerData.getCurrentValue(), playerData.getRewardReceive().toString(), playerData.getUpdateDateTime(), playerData.getPlayerIdentity(), playerData.getRankId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeRankPlayerData(PlayerData playerData) {
        String sql = "delete from ty_rank_player_data where player_identity = ? and rank_id = ?";
        try {
            TyRank.getBaseDao().executeUpdate(sql, playerData.getPlayerIdentity(), playerData.getRankId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
